﻿using System;
using System.Windows;
using Automator.Tasks.Core;
using Automator.Tasks.Windows;
using System.Windows.Media.Imaging;

namespace Automator.Tasks
{
    /// <summary>
    /// Class for logical grouping of Tasks. Doesn't do anything, just contains dependencies.
    /// Defined attributes:
    /// GroupDetails: some additional description of group
    /// </summary>
    public class GroupTask: Task
    {
        public GroupTask()
            : base()
        {
            this.Title = "Group";
            this.Description = "Tasks logical group";
            this.TaskImage = new BitmapImage(new Uri("/Automator.Tasks;component/Resources/group.png", UriKind.Relative));

            this.Attributes.Add(new TaskAttributeDefinition() { Key = "GroupDetails", Value = String.Empty });
        }

        public GroupTask(string title, string details = "")
            : this()
        {
            this.Title = title;
            if (!String.IsNullOrEmpty(details))
                this.Attributes["GroupDetails"].Value = details;
        }

        public override string CommandText
        {
            get
            {
                return this.Attributes["GroupDetails"].Value.ToString().Trim();
            }
            set { base.CommandText = value; }
        }
        
        public override bool EditTask()
        {
            base.EditTask();
            GroupTaskEditWindow window = new GroupTaskEditWindow();
            window.Owner = Application.Current.MainWindow;
            //performing binding to the header
            window.header.ContentPanel.DataContext = this;            
            window.pnlContent.DataContext = this;
            window.footer.BtnOK.Click += delegate { window.DialogResult = true; };
            window.edtGroupDetails.Text = this.Attributes["GroupDetails"].Value.ToString();
            
            if (window.ShowDialog() == true)
            {
                this.Attributes["GroupDetails"].Value = window.edtGroupDetails.Text.Trim();
                this.CommandText = String.Empty;
                return true;
            }
            else
                return false;
        }
    }
}
